package com.test.mq;

import java.util.Date;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Publisher {

	public static void main(String[] args) throws JMSException {
		TopicConnectionFactory connectionFactory;
		TopicConnection connection = null;
		TopicSession session = null;
		TopicPublisher publisher;

		try {
			connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
					ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL);
			connection = connectionFactory.createTopicConnection();
			connection.start();
			session = connection.createTopicSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic("mq.topic");
			publisher = session.createPublisher(topic);
			TextMessage message = session.createTextMessage("Message from Producer " + new Date());
			publisher.send(message);
			session.commit();
			System.out.println("Send Message: " + message.getText());
		} catch (Exception e) {
			session.rollback();
		} finally {
			if (connection != null)
				connection.close();
		}
	}
}